import type { MenuItem } from '../types/router';
import { hasAnyRole, hasAnyPermission } from './auth';
// 通过路由属性、用户权限等动态生成菜单
// 从路由生成菜单
export const generateMenuFromRoutes = (routes: any[], userRoles: string[], userPermissions: string[]): MenuItem[] => {
  const menuItems: MenuItem[] = [];

  routes.forEach(route => {
    const meta = route.meta as any;
    
    // 跳过隐藏的路由
    if (meta?.hidden) return;

    // 检查权限
    if (meta?.requiresAuth) {
      // 检查角色权限
      if (meta.roles && !hasAnyRole(meta.roles)) {
        return;
      }
      
      // 检查权限
      if (meta.permissions && !hasAnyPermission(meta.permissions)) {
        return;
      }
    }

    const menuItem: MenuItem = {
      key: route.name,
      title: meta?.title || route.name,
      icon: meta?.icon,
      path: route.path,
      hidden: meta?.hidden || false,
      order: meta?.order || 0,
      external: meta?.external || false,
      target: meta?.target || '_self'
    };

    // 处理子菜单
    if (route.children && route.children.length > 0) {
      const childMenus = generateMenuFromRoutes(route.children, userRoles, userPermissions);
      if (childMenus.length > 0) {
        menuItem.children = childMenus;
      }
    }

    menuItems.push(menuItem);
  });

  // 按order排序
  return menuItems.sort((a, b) => (a.order || 0) - (b.order || 0));
};

// 获取当前激活的菜单项
export const getActiveMenuKey = (currentPath: string, menuItems: MenuItem[]): string[] => {
  const activeKeys: string[] = [];

  const findActiveKey = (items: MenuItem[], path: string): boolean => {
    for (const item of items) {
      // 检查路径是否匹配
      if (item.path) {
        // 处理相对路径和绝对路径
        const itemPath = item.path.startsWith('/') ? item.path : '/' + item.path;
        const currentPath = path.startsWith('/') ? path : '/' + path;
        
        // 特殊处理：如果当前路径是根路径 "/" 且菜单项是 "main"，则匹配
        if (item.key === 'main' && (currentPath === '/' || currentPath === '/main' || currentPath === '')) {
          activeKeys.push(item.key);
          return true;
        }
        
        if (itemPath === currentPath || currentPath.startsWith(itemPath + '/')) {
          activeKeys.push(item.key);
          return true;
        }
      }
      
      // 递归检查子菜单
      if (item.children) {
        if (findActiveKey(item.children, path)) {
          activeKeys.push(item.key);
          return true;
        }
      }
    }
    return false;
  };

  findActiveKey(menuItems, currentPath);
  return activeKeys;
};

// 获取展开的菜单项
export const getOpenMenuKeys = (currentPath: string, menuItems: MenuItem[]): string[] => {
  const openKeys: string[] = [];

  const findOpenKeys = (items: MenuItem[], path: string): boolean => {
    for (const item of items) {
      if (item.children) {
        // 检查子菜单中是否有匹配的路径
        if (findOpenKeys(item.children, path)) {
          openKeys.push(item.key);
          return true;
        }
        // 检查当前路径是否以父路径开头
        if (item.path && path.startsWith(item.path + '/')) {
          openKeys.push(item.key);
          return true;
        }
      }
    }
    return false;
  };

  findOpenKeys(menuItems, currentPath);
  return openKeys;
};
